# http 服务端
import os
from http.server import HTTPServer, SimpleHTTPRequestHandler

class MyRequestHandler(SimpleHTTPRequestHandler):
    # 可以在这里添加自定义的请求处理方法
    def do_GET(self):
        # 简单的响应示例
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write(b"Hello, world! - GET")
    def do_POST(self):
        # 简单的响应示例
        content_length = int(self.headers['Content-Length'])
        print("Content Leng = "+ str(content_length))
        post_data = self.rfile.read(content_length)
        f = os.open("data.jpg", os.O_RDWR|os.O_CREAT|os.O_BINARY)
        os.write(f,post_data)
        os.close(f)
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write(b"Hello, world! - POST")        
 
def run(server_class=HTTPServer, handler_class=MyRequestHandler, port=8000):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    print(f'Starting httpd server on port {port}')
    httpd.serve_forever()
 
if __name__ == '__main__':
    run()